package com.qf.fmall2302.interceptors;


import cn.hutool.jwt.JWT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@Component
@Slf4j
public class JwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //0. 判断，如果是浏览器发送的  OPTION请求， 直接放行
        String method = request.getMethod();
        if (method.equals("OPTIONS")){
            return true;
        }

        //1. 获取 请求头中的token
        String token = request.getHeader("token");

        //2. 校验jwttoken
        if(token == null){
            log.info("没带token，拒绝访问");
            return false;
        }

        boolean validate = JWT.of(token)
                .setKey("qfsyjava2302".getBytes())
                .validate(0);


        return validate;
    }
}
